package com.jingzhi.huimiao;

import java.util.Calendar;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jingzhi.huimiao.adapter.FragmentAdapter;
import com.jingzhi.huimiao.utils.DataStoreUtil;

public class MainActivity extends FragmentActivity implements OnClickListener {
	public static final int TAB_RADIO1 = 0;
	public static final int TAB_RADIO2 = 1;
	public static final int TAB_RADIO3 = 2;
	public static final int TAB_RADIO4 = 3;

	private ViewPager viewPager;
	private LinearLayout radio1, radio2, radio3, radio4;
	private ImageView icon1, icon2, icon3, icon4;
	private TextView iconname1, iconname2, iconname3, iconname4;
	private LinearLayout iv_back_role;
	private int textids[];
	private int iconids[];
	private int[] icondrawids;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//存储已进入过主页面
		DataStoreUtil.putboolean(MainActivity.this, "hasInMain", true);
		
		
		initView();
		addListener();
	}

	private void initView() {

		textids = new int[] { R.id.iconname1, R.id.iconname2, R.id.iconname3,
				R.id.iconname4 };
		iconids = new int[] { R.id.icon1, R.id.icon2, R.id.icon3, R.id.icon4 };
		icondrawids = new int[] { R.drawable.main_share1,
				R.drawable.main_review, R.drawable.main_search,
				R.drawable.main_mine };

		viewPager = (ViewPager) findViewById(R.id.vp_content);
		radio1 = (LinearLayout) findViewById(R.id.radio1);
		radio2 = (LinearLayout) findViewById(R.id.radio2);
		radio3 = (LinearLayout) findViewById(R.id.radio3);
		radio4 = (LinearLayout) findViewById(R.id.radio4);

		icon1 = (ImageView) findViewById(R.id.icon1);
		icon2 = (ImageView) findViewById(R.id.icon2);
		icon3 = (ImageView) findViewById(R.id.icon3);
		icon4 = (ImageView) findViewById(R.id.icon4);

		iconname1 = (TextView) findViewById(R.id.iconname1);
		iconname2 = (TextView) findViewById(R.id.iconname2);
		iconname3 = (TextView) findViewById(R.id.iconname3);
		iconname4 = (TextView) findViewById(R.id.iconname4);

		iv_back_role = (LinearLayout) findViewById(R.id.iv_back_tomain);

		radio1.setOnClickListener(this);
		radio2.setOnClickListener(this);
		radio3.setOnClickListener(this);
		radio4.setOnClickListener(this);
		// iv_back_role.setOnClickListener(this);

		FragmentAdapter adapter = new FragmentAdapter(
				getSupportFragmentManager());
		viewPager.setAdapter(adapter);

		// 设置第一个按钮是被选中的
		icon1.setBackgroundResource(R.drawable.main_share);
		iconname1.setTextColor(Color.rgb(134, 134, 134));
		
		viewPager.setCurrentItem(TAB_RADIO1);
	}

	private void addListener() {

		// colorBack();

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {

				icon1.setBackgroundResource(R.drawable.main_share1);
				icon2.setBackgroundResource(R.drawable.main_review);
				icon3.setBackgroundResource(R.drawable.main_search);
				icon4.setBackgroundResource(R.drawable.main_mine);

				iconname1.setTextColor(Color.rgb(134, 134, 134));
				iconname2.setTextColor(Color.rgb(134, 134, 134));
				iconname3.setTextColor(Color.rgb(134, 134, 134));
				iconname4.setTextColor(Color.rgb(134, 134, 134));

				switch (position) {
				case 0:
					icon1.setBackgroundResource(R.drawable.main_share);
					iconname1.setTextColor(Color.rgb(234, 93, 91));
					break;
				case 1:
					icon2.setBackgroundResource(R.drawable.main_review1);
					iconname2.setTextColor(Color.rgb(234, 93, 91));
					break;
				case 2:
					icon3.setBackgroundResource(R.drawable.main_search1);
					iconname3.setTextColor(Color.rgb(234, 93, 91));
					break;
				case 3:
					icon4.setBackgroundResource(R.drawable.main_mine1);
					iconname4.setTextColor(Color.rgb(234, 93, 91));
					break;

				default:
					break;
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TextView textview = (TextView) findViewById(textids[arg0]);
				// textview.setTextColor(Color.rgb(134, 134, 134));
				// ImageView icon = (ImageView) findViewById(iconids[arg0]);
				// icon.setBackgroundResource(icondrawids[arg0]);
			}

		});

	}

	public void colorBack() {
		icon1.setBackgroundResource(R.drawable.main_share1);
		icon2.setBackgroundResource(R.drawable.main_review);
		icon3.setBackgroundResource(R.drawable.main_search);
		icon4.setBackgroundResource(R.drawable.main_mine);

		iconname1.setTextColor(0);
		iconname2.setTextColor(0);
		iconname3.setTextColor(0);
		iconname4.setTextColor(0);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.radio1:
			viewPager.setCurrentItem(TAB_RADIO1);
			break;
		case R.id.radio2:
			viewPager.setCurrentItem(TAB_RADIO2);
			break;
		case R.id.radio3:
			viewPager.setCurrentItem(TAB_RADIO3);
			break;
		case R.id.radio4:
			viewPager.setCurrentItem(TAB_RADIO4);
			break;
		case R.id.iv_back_tomain:
			startActivity(new Intent(MainActivity.this,
					ChooseRoleActivity.class));
			finish();
			break;

		default:
			break;
		}
	}
	public void setPage(int position) { viewPager.setCurrentItem(position);}
}